<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
        当前浏览器不支持Canvas，请更换浏览器
    </canvas>
    <script>
        window.onload = function () {
            var canvas = document.getElementById("canvas");

            canvas.width = 800;
            canvas.height = 800;

            var context = canvas.getContext("2d");

            context.fillStyle = '#058';
            context.font = 'bold 40px sans-serif';
            context.fillText("欢迎大家学习《Canvas》绘图接口详解！", 40, 100);

            var textWidth = context.measureText("欢迎大家学习《Canvas》绘图接口详解！").width;
            context.fillText("以上字符串的宽度为 "+textWidth+"px", 40, 200);

        }
    </script>
</body>
</html>